package com.jiazhong.Dao.Impl;

import com.jiazhong.Dao.DBUtils;
import com.jiazhong.Dao.GoodsDao;
import com.jiazhong.Model.Goods;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class GoodsDaoImpl extends DBUtils implements GoodsDao {
    @Override
    public List<Goods> queryGoods() {
        String sql="select * from tbl_goods";
        return super.executeQueryList(Goods.class,sql);


        //测试DBUtils查询结果数量类
        /*String sql="select count(*) from tbl_goods";
        Long count = super.executeUpdateQueryCount(sql);
        System.out.println(count);
        return null;*/

        //未使用DBUtils查询
//        try {
//            String sql="select * from tbl_goods";
//            super.getConn();
//            ps = conn.prepareStatement(sql);
//            rs = ps.executeQuery();
//            List<Goods> goodsList=new ArrayList<>();
//            while(rs.next()){
//                Goods goods=new Goods();
//                goods.setGoods_id(rs.getInt(1));
//                goods.setGoods_name(rs.getString(2));
//                goods.setGoods_price(rs.getDouble(3));
//                goods.setGoods_time(rs.getDate(4));
//                goodsList.add(goods);
//            }
//            return goodsList;
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } finally {
//            super.closeAll();
//        }
//        return null;
    }

    @Override
    public void delGoodsById(int goods_id) {
        String sql="delete from tbl_goods where goods_id=?";
        super.executeUpdate(sql,goods_id);
    }

    @Override
    public Goods queryGoodsById(int goods_id) {
        try {
            String sql="select * from tbl_goods where goods_id=?";
            super.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,goods_id);
            rs=ps.executeQuery();
            while (rs.next()){
                Goods goods=new Goods();
                goods.setGoods_id(rs.getInt(1));
                goods.setGoods_name(rs.getString(2));
                goods.setGoods_price(rs.getDouble(3));
                goods.setGoods_time(rs.getDate(4));
                return goods;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll();
        }
        return null;
    }

    @Override
    public void updateGoods(Goods goods) {
        String sql="update tbl_goods set goods_name=?,goods_price=?,goods_time=? where goods_id=?";
        super.executeUpdate(sql,goods.getGoods_name(),goods.getGoods_price(),goods.getGoods_time(),goods.getGoods_id());

    }
}
